/*
 * @Author: HX
 * @Date: 2024-12-11 10:56:00
 * @LastEditors: HX
 * @LastEditTime: 2024-12-11 16:54:07
 */
let PostProcess = {
  add() {
    const shaderCode = this.getshaderCode();
    const material = new SSmap.Material();
    var fields = new SSmap.StringVector();
    material.shaderProgram =
      SSmap.ShaderProgram.createViewportQuadShader(
        shaderCode,
        fields
      );
    fields.delete();
    const postProcess = new SSmap.PostProcess();
    postProcess.material = material;
    window.viewer.renderSystem.addPostProcess(postProcess);
    const maxRadius = 1500.0; //最大半径
    const insideRadius = 300.0; //内圈半径

    const duration = 2.0; //控制半径  扩散  持续时间
    var timer = 0;

    // 不需要动态即可使用以下代码
    let frameAction = window.viewer.addEventListener(
      "update",
      function (dt) {
        const viewMatrix =
          window.viewer.scene.mainCamera.viewMatrix();
        const center = SSmap.Matrix4.multiplyByVector3(
          viewMatrix,
          SSmap.Cartesian3.fromDegrees(
            119.18366081206689,
            29.360837339013237,
            83.23739076322025
          ).toVector3()
        );
        material.setParameterVec3("centerEC", center); //中心点
        material.setParameterFloat("u_radius", maxRadius); //变化 半径
        material.setParameterFloat("u_innerRadius", insideRadius); //变化 半径
        material.setParameterColor(
          "u_scanColor",
          SSmap.Color.fromRgb(20, 20, 20, 255)
        ); //颜色
        material.setParameterColor(
          "u_innerColor",
          SSmap.Color.fromRgb(0, 0, 0, 111)
        ); //颜色

      }
    );
  },
  getshaderCode() {
    let constantval = window.isDeskTop ? '' : `#version 300 es
      precision highp float;
      precision highp sampler2D;`; //常数定义 客户端不需要应用
    return (
      constantval +
      `

  layout(location = 0) out vec4 FragColor;

  in vec2 v_textureCoordinates;

  uniform sampler2D colorTexture;       // 内置颜色缓存
  uniform sampler2D depthTexture;       // 内置深度缓存
  uniform mat4 inverseViewMatrix;       // 内置变量(ViewSpace->WorldSpace)
  uniform vec4 nearPlanes;              // 内置变量(近面视口坐标L,B,R,T)
  uniform vec3 centerEC;               // 中心
  uniform float u_radius;             // 扫描半径
  uniform float u_innerRadius;         // 内部空心半径
  uniform vec4 u_scanColor;           // 扫描颜色
  uniform vec4 u_innerColor;           // 扫描颜色

  vec3 computePositionEC(vec2 uv, float depth)
  {
      vec3 pos = vec3(mix(nearPlanes.xy, nearPlanes.zw, uv), -1.0);
      return pos * depth;
  }

  void main()
  {
      vec2 uv = v_textureCoordinates;

      float depth = texture(depthTexture, uv).x;
      vec3 positionEC = computePositionEC(uv, depth);

      float dist = length(positionEC - centerEC);
      vec4 color = texture(colorTexture, uv);


      if (dist > u_radius)
      {
        FragColor = mix(color, u_scanColor,0.3);
      }

      if (dist < u_radius)
      {
        float f = dist/u_radius;
        f = pow(f, 2.0);
        FragColor = color ;
      }
      if (dist > u_innerRadius && dist < u_radius)
      {
          float f = (dist - u_innerRadius) / (u_radius - u_innerRadius);
          f = pow(f, 2.0);
          FragColor = mix(color, mix(color, u_scanColor,0.3), f);
      }

  }
  `
    );
  }
}

export { PostProcess }